var api = require('./commapi.js');

var FILECONSTANT = 
{
	//-文件记录-
	FILEFLD_DT		:	0,	//时间
	FILEFLD_TYPE	:	1,	//类型
	FILEFLD_SRCNAME	:	2,	//原始文件名
	FILEFLD_DESTNAME:	3,	//目标文件名
	FILEFLD_DELFLAG	:	4,	//标记(1-del)
	FILEFLD_MODCOUNT:	5,	//修改次数

	version:	"20111228",
	author:		"mr.7"	
};

var	file = 
{
	//-打开-	
	open : function(path)
	{
		this._strDBDir = path;
		this._strDBFile= path + "/file.db";

		//-打开文件库-
		this._fileMap = require('./map.js').Map;		
		_openFile(this);

		function _openFile(obj)
		{
			var data = api.loadFile(obj._strDBFile);
			if(data=="")
				return;
			var arr = data.split(/\r\n/);
			for(var i=0; i<arr.length; i++)
			{
				var rcd = arr[i].split("||");				
				rcd[FILECONSTANT.FILEFLD_DELFLAG] = parseInt(rcd[FILECONSTANT.FILEFLD_DELFLAG]);
				rcd[FILECONSTANT.FILEFLD_MODCOUNT] = parseInt(rcd[FILECONSTANT.FILEFLD_MODCOUNT]);
				obj.fAdd(rcd);
			}
		}
	},

	//-保存-
	save:function(bKeepDel)
	{
		var data = "";
		var i,v;
		for(i=0; i<this._fileRst.length; i++)
		{
			v = this._fileRst[i];
			
			//-如果删除，忽略-
			if(bKeepDel!=true && v[FILECONSTANT.FILEFLD_DELFLAG]==1)
				continue;

			//-保存当前记录-
			if(data!="")
				data +="\r\n";
			data += v.join("||");
		}
		
		//-保存文件-
		api.saveFile(this._strDBFile, data);
	},
		
	//-关闭-	
	close : function()
	{
		if(this._fileMap)
			this._fileMap.clear();
		this._fileRst.length = 0;
		
		this._strDBDir = "";
		this._strDBFile = "";
	},
	
	//-添加一条记录-
	fAdd : function(rcdfile)
	{
		this._fileRst.push(rcdfile);
		this._fileMap.put(rcdfile[FILECONSTANT.FILEFLD_SRCNAME], this._fileRst.length-1);
	},

	fAddRcdInfo : function(type,filename,suffix, destFileName)
	{
		//-添加文件信息-
		var rcd = [];		
		var dt = new Date();
		var key = this.fGetKey(type,filename,suffix);
		rcd.push(api.dtToString(dt), type, key, destFileName, 0, 1);
		this.fAdd(rcd);
		return this._fileRst[this._fileRst.length-1];
	},

	//-获取文件名在数据库中的key-
	fGetKey : function(type, filename, suffix)
	{
		var key = type+filename;
		if(suffix!=undefined && suffix.length!=0)
		{
			key += ".";
			key += suffix;
		}

		return key;
	},

	//-获取文件信息-
	fGet : function(type, filename, suffix)
	{
		var rcdfile;
		var key = this.fGetKey(type, filename, suffix);
		var value = this._fileMap.get(key);
		if(value)
			rcdfile = this._fileRst[value];
		return rcdfile;
	},

	//-获取文件所在的路径-
	_fGetShortPath : function(rcdfile)
	{
		var strRtn;
		var strPath = rcdfile[FILECONSTANT.FILEFLD_DESTNAME];
		var nPos = strPath.lastIndexOf("/");
		if(nPos>=0)
			strRtn = strPath.substr(0, nPos);
		return strRtn;
	},

	fGetShortPath : function(type, filename, suffix)
	{
		var strRtn;
		var rcdfile = this.fGet(type, filename, suffix);
		if(rcdfile)
			strRtn = this._fGetShortPath(rcdfile);

		return strRtn;
	},

	//-根据文件库中相对路径获取完整路径-
	fGetAbsolutePath : function(curpath)
	{
		return this._strDBDir+curpath.substr(1);
	},

	//-获取文件实际名,绝对路径-
	_fGetDestFileName: function(rcdfile)
	{
		return this.fGetAbsolutePath(rcdfile[FILECONSTANT.FILEFLD_DESTNAME]);
	},

	fGetDestFileName : function(type, filename, suffix)
	{
		var destFileName;
		var rcd = this.fGet(type, filename, suffix);
		if(rcd)
			destFileName = this._fGetDestFileName(rcd);
		return destFileName;
	},

	//-上传文件-
	fAddFile : function(data, type, filename, suffix, destFileName)
	{
		//-上传文件-
		var strAbsolutePath = this.fGetAbsolutePath(destFileName);
		api.saveFile(strAbsolutePath, data);

		//-添加文件信息-
		return this.fAddRcdInfo(type,filename,suffix, destFileName);
	},

	//-更新文件-
	fUpdateFile : function(data, type, filename, suffix)
	{	
		//-获取原始文件名,已经是绝对路径-
		var	strAbsolutePath = this.fGetDestFileName(type, filename, suffix);
		//-上传文件-
		api.saveFile(strAbsolutePath, data);

		//-获取原始文件信息-
		var rcd = this.fGet(type, filename, suffix);

		//-更新文件信息-
		var dt = new Date();
		rcd[FILECONSTANT.FILEFLD_DT]  = api.dtToString(dt);
		rcd[FILECONSTANT.FILEFLD_MODCOUNT] += 1;
	},

	//-删除文件-
	_fDelFile : function(rcdfile)
	{
		rcdfile[FILECONSTANT.FILEFLD_DELFLAG] = 1;
		return true;
	},

	fDelFile : function(type, filename, suffix)
	{
		var rtn = false;
		var rcdfile = this.fGet(type, filename, suffix);
		if(rcdfile)
			rtn = this._fDelFile(rcdfile);			
		return rtn;
	},
	
	_fileMap :	null,
	_fileRst :	[],
	
	_strDBDir: "",				//路径
	_strDBFile:"",				//数据库文件

	version:	"20111227",
	author:		"mr.7"
};

exports.file = file;